package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.*;
import com.qf.service.*;
import com.qf.utils.DataSourceUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private UserService userService = new UserServiceImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            DataSourceUtil.begin();
            orderDao.add(order);
            for (Cart cart :cartList) {
                OrderDetail orderDetail = new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDao.addDetail(orderDetail);

                CartService cartService = new CartServiceImpl();
                cartService.clear(order.getUid());
                DataSourceUtil.commit();
            }
        } catch (SQLException throwables) {
            try {
                DataSourceUtil.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throw  new RuntimeException(throwables);
        } finally {
            try {
                DataSourceUtil.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public List<Order> find(Integer uid) {
        List<Order> orderList = orderDao.select(uid);
        if (orderList!=null){
            AddressService addressService = new AddressServiceImpl();
            List<Address> addressList = addressService.find(uid);
            if (addressList!=null){
                for (Order order:orderList){
                    for (Address address:addressList){
                            if (order.getUid()==address.getId()){
                                order.setAddress(address);
                            }
                    }
                }
            }
        }
        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
        Order order = orderDao.selectByoid(oid);
        if (order!=null){
                //手动封装地址信息
                AddressService addressService = new AddressServiceImpl();
                List<Address> addressList = addressService.find(order.getUid());
                if (addressList != null){
                    for (Address address : addressList){
                        if (address.getId()==order.getAid()){
                            order.setAddress(address);
                        }
                    }
                }
        }
        return order;
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail> orderDetails = orderDao.selectDetail(oid);
        if (orderDetails != null&&orderDetails.size()>=0){
            GoodsService goodsService = new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails){
                Goods goods = goodsService.findById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }

    @Override
    public List<Order> selectorder() {
        ArrayList<Order> list = (ArrayList<Order>) orderDao.selectLIST();
        for (Order order:list) {
            User user = userService.selectById(order.getUid());
            order.setUsername(user.getUsername());
        }
        return list;
    }
}
